package com.metadata_system.ai.response;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.metadata_system.common.db.entity.MetadataNode;
import com.metadata_system.system.response.NodePropertiesItem;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@AllArgsConstructor
@NoArgsConstructor
@Builder
@Data
public class MetadataNodeResponse {
    private Integer id;
    private Integer metadataId;
    private Integer metadataViewId;
    private Integer dataTotal;
    private List<NodePropertiesItem> properties;
    private String name;
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date createdTime;
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date updatedTime;

    public static MetadataNodeResponse fromEntity(MetadataNode metadata) {
        return MetadataNodeResponse.builder()
                .id(metadata.getId())
                .metadataId(metadata.getMetadataId())
                .metadataViewId(metadata.getMetadataViewId())
                .dataTotal(metadata.getDataTotal())
                .properties(JSON.parseArray(metadata.getProperties(), NodePropertiesItem.class))
                .name(metadata.getName())
                .createdTime(metadata.getCreatedTime())
                .updatedTime(metadata.getUpdatedTime())
                .build();
    }

    public static List<MetadataNodeResponse> fromEntity(List<MetadataNode> metadataList) {
        return metadataList.stream().map(MetadataNodeResponse::fromEntity).collect(Collectors.toList());
    }
}
